package cn.zy.shardbatisdemo.service;

import cn.zy.shardbatisdemo.mapper.ShardMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import cn.zy.shardbatisdemo.entity.Shard;
import org.springframework.util.StringUtils;

@Service
public class ShardServiceImpl implements ShardService{

    @Resource
    private ShardMapper shardMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return shardMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Shard record) {
        return shardMapper.insert(record);
    }

    @Override
    public int insertSelective(Shard record) {
        return shardMapper.insertSelective(record);
    }

    @Override
    public Shard selectByPrimaryKey(String id) {
        return shardMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Shard record) {
        return shardMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Shard record) {
        return shardMapper.updateByPrimaryKey(record);
    }

    @Override
    public String getName(String id) {
        Shard shard = shardMapper.getShardResult(id);//执行到这条语句时，会被拦截进入到分表策略MyShardStrategy.java中，获取对应的表名称，并且更改sql中的表名
        return StringUtils.isEmpty(shard.getName())?null:shard.getName();
    }
}
